package com.app.controller;

import com.app.common.Result;
import com.app.model.AddressModel;
import com.app.model.UserModel;
import com.app.service.AddressService;
import com.app.service.UserService;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.AddressVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 地址信息
 *
 * @author 16693103420
 * @since 2024-02-01
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;

    // 查询列表
    @GetMapping("/getAddressList")
    public Result getAddressList(@RequestParam(defaultValue = "0") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit) {
        AddressVo addressVo = addressService.getAddressList(page, limit);
        return Result.success(addressVo);
    }

    // 查询用户列表
    @GetMapping("/getUserAddressList")
    public Result getUserAddressList(@RequestParam(defaultValue = "0") Integer page,
                                     @RequestParam(defaultValue = "10") Integer limit, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        Integer userId = user.getId();
        AddressVo addressVo = addressService.getUserAddressList(page, limit, userId);
        return Result.success(addressVo);
    }

    //根据id查询
    @GetMapping("/getAddressById")
    public Result getAddressById(Integer id) {
        AddressModel address = addressService.getById(id);
        return Result.success(address);
    }

    //新增和修改
    @PostMapping("/addAndUpdateAddress")
    public Result addAndUpdateAddress(@RequestBody AddressModel addressModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (addressModel.getId() != null && addressModel.getId() > 0) {
            if (addressModel.getIsDefault() == 1) {
                //先将此用户的所有地址设置为2
                UpdateWrapper<AddressModel> uw = new UpdateWrapper<>();
                uw.eq("user_id", user.getId());
                uw.set("is_default", 2);
                addressService.update(uw);
                //再将当前地址的id设置为1
                addressModel.setIsDefault(1);
            }
            addressModel.setUpdateTime(CommonHelp.getNowTimestamp());
            addressService.updateById(addressModel);
            return Result.success("修改成功");
        } else {
            Integer isDefault = addressModel.getIsDefault();
            if (isDefault == 1) {
                //先将此用户的所有地址设置为2
                UpdateWrapper<AddressModel> uw = new UpdateWrapper<>();
                uw.eq("user_id", user.getId());
                uw.set("is_default", 2);
                addressService.update(uw);
                //再将当前地址的id设置为1
                addressModel.setIsDefault(1);
            }
            addressModel.setUserId(user.getId());
            addressModel.setCreateTime(CommonHelp.getNowTimestamp());
            addressService.save(addressModel);
            return Result.success("添加成功");
        }
    }

    //设置默认地址
    @PostMapping("/updateDefaultAddress")
    public Result updateDefaultAddress(@RequestBody AddressModel addressModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        UpdateWrapper<AddressModel> uw = new UpdateWrapper<>();
        uw.eq("user_id", user.getId());
        uw.set("is_default", 2);
        addressService.update(uw);
        //再将当前地址的id设置为1
        addressModel.setIsDefault(1);
        addressService.updateById(addressModel);
        return Result.success("设置默认地址成功");
    }

    //根据id删除
    @GetMapping("/delAddress")
    public Result delAddress(Integer id) {
        AddressModel addressModel = addressService.getById(id);
        if (addressModel != null) {
            addressModel.setStatus(2);
            addressService.updateById(addressModel);
        }
        return Result.success("删除成功");
    }
}

